<template>
  <div class="home-container">
    <!-- 顶部导航 -->
    <HomeHeader />
    
    <!-- 主要内容区域 -->
    <main class="home-main">
      <!-- 英雄区域 -->
      <section class="hero-section">
        <div class="hero-content">
          <div class="hero-text">
            <h1 class="hero-title">
              <span class="gradient-text">ZenithMind</span>
              <br>在线教育一体化平台
            </h1>
            <p class="hero-subtitle">
              智能学习，成就未来 - 集成15个微服务的综合性在线教育平台
            </p>
            <div class="hero-features">
              <div class="feature-item">
                <el-icon><VideoPlay /></el-icon>
                <span>智能课堂</span>
              </div>
              <div class="feature-item">
                <el-icon><EditPen /></el-icon>
                <span>在线考试</span>
              </div>
              <div class="feature-item">
                <el-icon><Monitor /></el-icon>
                <span>编程实战</span>
              </div>
              <div class="feature-item">
                <el-icon><ChatDotRound /></el-icon>
                <span>AI助手</span>
              </div>
            </div>
            <div class="hero-actions">
              <el-button 
                type="primary" 
                size="large" 
                @click="startLearning"
                class="start-btn"
              >
                <el-icon><Right /></el-icon>
                开始学习
              </el-button>
              <el-button 
                size="large" 
                @click="exploreFeatures"
                class="explore-btn"
              >
                <el-icon><View /></el-icon>
                探索功能
              </el-button>
            </div>
          </div>
          <div class="hero-visual">
            <div class="hero-image">
              <div class="placeholder-hero">智能学习平台</div>
            </div>
            <div class="floating-cards">
              <div class="card card-1" @click="handleCardClick('progress')">
                <el-icon><TrendCharts /></el-icon>
                <span>学习进度</span>
              </div>
              <div class="card card-2" @click="handleCardClick('achievement')">
                <el-icon><Trophy /></el-icon>
                <span>成就系统</span>
              </div>
              <div class="card card-3" @click="handleCardClick('recommend')">
                <el-icon><User /></el-icon>
                <span>个性化推荐</span>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 功能模块展示 -->
      <section class="modules-section">
        <div class="section-header">
          <h2>全方位学习体验</h2>
          <p>15个专业模块，满足您的所有学习需求</p>
        </div>
        <div class="modules-grid">
          <div 
            v-for="module in modules" 
            :key="module.id"
            class="module-card"
            @click="navigateToModule(module)"
          >
            <div class="module-icon">
              <el-icon :size="32">
                <component :is="module.icon" />
              </el-icon>
            </div>
            <h3>{{ module.name }}</h3>
            <p>{{ module.description }}</p>
            <div class="module-stats">
              <span>{{ module.userCount }}+ 用户</span>
              <span>{{ module.contentCount }}+ 内容</span>
            </div>
          </div>
        </div>
      </section>

      <!-- 数据统计 -->
      <section class="stats-section">
        <div class="stats-container">
          <div class="stat-item">
            <div class="stat-number">{{ formatNumber(stats.totalUsers) }}</div>
            <div class="stat-label">注册用户</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ formatNumber(stats.totalCourses) }}</div>
            <div class="stat-label">优质课程</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ formatNumber(stats.totalStudyTime) }}</div>
            <div class="stat-label">学习时长(小时)</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ formatNumber(stats.totalCertificates) }}</div>
            <div class="stat-label">颁发证书</div>
          </div>
        </div>
      </section>

      <!-- 热门课程 -->
      <section class="courses-section">
        <div class="section-header">
          <h2>热门课程推荐</h2>
          <p>精选优质课程，助力您的学习之路</p>
        </div>
        <div class="courses-grid">
          <div 
            v-for="course in hotCourses" 
            :key="course.id"
            class="course-card"
            @click="viewCourse(course)"
          >
            <div class="course-image">
              <img :src="course.cover" :alt="course.title" />
              <div class="course-badge">{{ course.category }}</div>
            </div>
            <div class="course-content">
              <h3>{{ course.title }}</h3>
              <p>{{ course.description }}</p>
              <div class="course-meta">
                <div class="course-rating">
                  <el-rate 
                    v-model="course.rating" 
                    disabled 
                    show-score 
                    text-color="#ff9900"
                    score-template="{value}"
                  />
                </div>
                <div class="course-students">
                  <el-icon><User /></el-icon>
                  {{ course.studentCount }}
                </div>
              </div>
              <div class="course-price">
                <span v-if="course.price === 0" class="free">免费</span>
                <span v-else class="price">¥{{ course.price }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="section-footer">
          <el-button type="primary" @click="viewAllCourses">
            查看全部课程
            <el-icon><ArrowRight /></el-icon>
          </el-button>
        </div>
      </section>

      <!-- 最新资讯 -->
      <section class="news-section">
        <div class="section-header">
          <h2>最新资讯</h2>
          <p>了解教育行业最新动态</p>
        </div>
        <div class="news-grid">
          <div 
            v-for="news in latestNews" 
            :key="news.id"
            class="news-card"
            @click="viewNews(news)"
          >
            <div class="news-image">
              <img :src="news.cover" :alt="news.title" />
            </div>
            <div class="news-content">
              <div class="news-category">{{ news.category }}</div>
              <h3>{{ news.title }}</h3>
              <p>{{ news.summary }}</p>
              <div class="news-meta">
                <span class="news-date">{{ formatDate(news.publishTime) }}</span>
                <span class="news-views">{{ news.viewCount }} 阅读</span>
              </div>
            </div>
          </div>
        </div>
      </section>
    </main>

    <!-- 底部 -->
    <HomeFooter />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { 
  VideoPlay, EditPen, Monitor, ChatDotRound, Right, View,
  TrendCharts, Trophy, User, ArrowRight
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 组件
import HomeHeader from './components/HomeHeader.vue'
import HomeFooter from './components/HomeFooter.vue'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const stats = ref({
  totalUsers: 125000,
  totalCourses: 8500,
  totalStudyTime: 2800000,
  totalCertificates: 45000
})

const modules = ref([
  {
    id: 'classroom',
    name: '智能课堂',
    description: '在线视频学习，智能推荐课程',
    icon: 'VideoPlay',
    userCount: 50000,
    contentCount: 2000,
    route: '/classroom'
  },
  {
    id: 'exam',
    name: '在线考试',
    description: '智能组卷，实时评分',
    icon: 'EditPen',
    userCount: 35000,
    contentCount: 1500,
    route: '/exam'
  },
  {
    id: 'coding',
    name: '编程实战',
    description: '在线编程，代码评测',
    icon: 'Monitor',
    userCount: 28000,
    contentCount: 800,
    route: '/coding'
  },
  {
    id: 'ai-chat',
    name: 'AI助手',
    description: '智能问答，学习辅导',
    icon: 'ChatDotRound',
    userCount: 45000,
    contentCount: 0,
    route: '/ai-chat'
  },
  {
    id: 'task',
    name: '学习任务',
    description: '任务管理，进度跟踪',
    icon: 'List',
    userCount: 32000,
    contentCount: 1200,
    route: '/task'
  },
  {
    id: 'library',
    name: '数字图书馆',
    description: '海量图书，在线阅读',
    icon: 'Reading',
    userCount: 40000,
    contentCount: 5000,
    route: '/library'
  }
])

const hotCourses = ref([])
const latestNews = ref([])

// 方法
const startLearning = () => {
  if (userStore.isLoggedIn) {
    router.push('/dashboard')
  } else {
    router.push('/login')
  }
}

const exploreFeatures = () => {
  // 滚动到功能模块区域
  document.querySelector('.modules-section').scrollIntoView({
    behavior: 'smooth'
  })
}

const navigateToModule = (module) => {
  router.push(module.route)
}

const viewCourse = (course) => {
  router.push(`/classroom/course/${course.id}`)
}

const viewAllCourses = () => {
  router.push('/classroom/courses')
}

const viewNews = (news) => {
  router.push(`/news/detail/${news.id}`)
}

const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toLocaleString()
}

const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

const handleCardClick = (type) => {
  if (userStore.isLoggedIn) {
    switch (type) {
      case 'progress':
        router.push('/dashboard/progress');
        break;
      case 'achievement':
        router.push('/dashboard/achievements');
        break;
      case 'recommend':
        router.push('/dashboard/recommendations');
        break;
      default:
        router.push('/dashboard');
    }
  } else {
    ElMessage({
      message: '请先登录以查看详细内容',
      type: 'info'
    });
    router.push('/login');
  }
}

const loadData = async () => {
  try {
    // 加载热门课程
    // const coursesRes = await courseApi.getHotCourses({ limit: 6 })
    // hotCourses.value = coursesRes.data
    
    // 加载最新资讯
    // const newsRes = await newsApi.getLatestNews({ limit: 4 })
    // latestNews.value = newsRes.data
    
    // 模拟数据
    hotCourses.value = [
      {
        id: 1,
        title: 'Vue3 + TypeScript 实战开发',
        description: '从零开始学习Vue3和TypeScript，构建现代化前端应用',
        cover: '/images/course-1.jpg',
        category: '前端开发',
        rating: 4.8,
        studentCount: 1250,
        price: 199
      },
      // 更多课程...
    ]
    
    latestNews.value = [
      {
        id: 1,
        title: '2024年在线教育发展趋势报告',
        summary: '深度分析在线教育行业的最新发展趋势和未来展望',
        cover: '/images/news-1.jpg',
        category: '行业报告',
        publishTime: '2024-01-15',
        viewCount: 2580
      },
      // 更多资讯...
    ]
  } catch (error) {
    console.error('加载数据失败:', error)
  }
}

onMounted(() => {
  loadData()
})
</script>

<style lang="scss" scoped>
.home-container {
  min-height: 100vh;
  background: white;
}

.home-main {
  padding-top: 60px; // 匹配头部高度
}

// 英雄区域样式
.hero-section {
  padding: 80px 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  
  .hero-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 60px;
    align-items: center;
    
    @media (max-width: 992px) {
      grid-template-columns: 1fr;
      gap: 40px;
    }
  }
  
  .hero-title {
    font-size: 3.5rem;
    font-weight: 700;
    line-height: 1.2;
    margin-bottom: 20px;
    
    @media (max-width: 768px) {
      font-size: 2.5rem;
    }
    
    .gradient-text {
      background: linear-gradient(45deg, #fff, #f0f8ff);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }
  }
  
  .hero-subtitle {
    font-size: 1.25rem;
    line-height: 1.6;
    margin-bottom: 40px;
    opacity: 0.9;
  }
  
  .hero-features {
    display: flex;
    flex-wrap: wrap;
    gap: 30px;
    margin-bottom: 40px;
    
    .feature-item {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 0.9rem;
      opacity: 0.8;
    }
  }
  
  .hero-actions {
    display: flex;
    gap: 20px;
    
    @media (max-width: 480px) {
      flex-direction: column;
      width: 100%;
    }
    
    .start-btn {
      padding: 12px 30px;
      font-size: 1.1rem;
      border-radius: 25px;
    }
    
    .explore-btn {
      padding: 12px 30px;
      font-size: 1.1rem;
      border-radius: 25px;
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.3);
      color: white;
      
      &:hover {
        background: rgba(255, 255, 255, 0.2);
      }
    }
  }
}

.hero-image {
  position: relative;
  max-width: 600px;
  width: 100%;
  
  .placeholder-hero {
    width: 100%;
    height: 350px;
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: bold;
    color: white;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  }
}

.floating-cards {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 15px;
  z-index: 999;
  pointer-events: auto;
  
  .card {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    padding: 12px 16px;
    width: 140px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    color: #333;
    font-weight: 500;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    cursor: pointer;
    
    .el-icon {
      font-size: 18px;
    }
    
    span {
      font-size: 14px;
      white-space: nowrap;
    }
    
    &:hover {
      transform: translateX(-5px);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    }
  }
}

@keyframes float {
  0% { transform: translateY(0); }
  50% { transform: translateY(0); }
  100% { transform: translateY(0); }
}

// 功能模块区域
.modules-section {
  padding: 80px 0;
  background: white;
  
  .section-header {
    text-align: center;
    max-width: 700px;
    margin: 0 auto 60px;
    
    h2 {
      font-size: 2.5rem;
      font-weight: 700;
      margin-bottom: 16px;
      color: #333;
    }
    
    p {
      font-size: 1.1rem;
      color: #666;
    }
  }
  
  .modules-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 30px;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }
  
  .module-card {
    background: #fff;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    cursor: pointer;
    
    &:hover {
      transform: translateY(-10px);
      box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
    }
    
    .module-icon {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 70px;
      height: 70px;
      border-radius: 20px;
      background: #f0f5ff;
      color: #6675f7;
      margin-bottom: 24px;
    }
    
    h3 {
      font-size: 1.25rem;
      font-weight: 600;
      margin-bottom: 12px;
      color: #333;
    }
    
    p {
      font-size: 0.95rem;
      color: #666;
      margin-bottom: 24px;
      line-height: 1.6;
    }
    
    .module-stats {
      display: flex;
      justify-content: space-between;
      font-size: 0.85rem;
      color: #888;
    }
  }
}

// 数据统计区域
.stats-section {
  padding: 60px 0;
  background: #f8faff;
  
  .stats-container {
    display: flex;
    justify-content: space-between;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    
    @media (max-width: 768px) {
      flex-wrap: wrap;
      gap: 30px;
    }
  }
  
  .stat-item {
    text-align: center;
    
    @media (max-width: 768px) {
      width: calc(50% - 15px);
    }
    
    .stat-number {
      font-size: 3rem;
      font-weight: 700;
      background: linear-gradient(45deg, #6675f7, #764ba2);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      margin-bottom: 8px;
    }
    
    .stat-label {
      font-size: 1rem;
      color: #666;
      font-weight: 500;
    }
  }
}

// 课程区域
.courses-section {
  padding: 80px 0;
  background: white;
  
  .section-header {
    text-align: center;
    max-width: 700px;
    margin: 0 auto 60px;
    
    h2 {
      font-size: 2.5rem;
      font-weight: 700;
      margin-bottom: 16px;
      color: #333;
    }
    
    p {
      font-size: 1.1rem;
      color: #666;
    }
  }
  
  .courses-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 30px;
    max-width: 1200px;
    margin: 0 auto 40px;
    padding: 0 20px;
  }
  
  .course-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
    
    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
    }
    
    .course-image {
      position: relative;
      height: 200px;
      background: #e5e5e5;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      
      .course-badge {
        position: absolute;
        top: 16px;
        right: 16px;
        padding: 6px 12px;
        background: rgba(0, 0, 0, 0.7);
        color: white;
        border-radius: 20px;
        font-size: 0.8rem;
      }
    }
    
    .course-content {
      padding: 24px;
      
      h3 {
        font-size: 1.2rem;
        font-weight: 600;
        margin-bottom: 10px;
      }
      
      p {
        font-size: 0.9rem;
        color: #666;
        margin-bottom: 16px;
        line-height: 1.6;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      
      .course-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        
        .course-rating {
          display: flex;
          align-items: center;
        }
        
        .course-students {
          display: flex;
          align-items: center;
          gap: 6px;
          font-size: 0.9rem;
          color: #666;
        }
      }
      
      .course-price {
        font-weight: 600;
        font-size: 1.1rem;
        
        .free {
          color: #4caf50;
        }
        
        .price {
          color: #f44336;
        }
      }
    }
  }
  
  .section-footer {
    text-align: center;
    margin-top: 40px;
  }
}

// 资讯区域
.news-section {
  padding: 80px 0;
  background: #f8faff;
  
  .section-header {
    text-align: center;
    max-width: 700px;
    margin: 0 auto 60px;
    
    h2 {
      font-size: 2.5rem;
      font-weight: 700;
      margin-bottom: 16px;
      color: #333;
    }
    
    p {
      font-size: 1.1rem;
      color: #666;
    }
  }
  
  .news-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 30px;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }
  
  .news-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
    
    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
    }
    
    .news-image {
      height: 180px;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    
    .news-content {
      padding: 24px;
      
      .news-category {
        display: inline-block;
        padding: 4px 10px;
        background: #e3f2fd;
        color: #1976d2;
        border-radius: 20px;
        font-size: 0.8rem;
        margin-bottom: 12px;
      }
      
      h3 {
        font-size: 1.2rem;
        font-weight: 600;
        margin-bottom: 10px;
      }
      
      p {
        font-size: 0.9rem;
        color: #666;
        margin-bottom: 16px;
        line-height: 1.6;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      
      .news-meta {
        display: flex;
        justify-content: space-between;
        font-size: 0.85rem;
        color: #888;
      }
    }
  }
}
</style>
